package crawler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;

public class Fetcher {
	private HttpURLConnection connection;
	private MessageBox msgBox;

	public Fetcher() {
		connection = null;
		System.setProperty("http.proxyHost","proxy.tuiasi.ro");
		System.setProperty("http.proxyPort","8080");

	}

	public String doRequestURL(URL url) {
		//System.out.println("Fetcher.doRequestURL: " + url);
		// TODO: Store that this URL has been visited already + time stamp
		String rawHttp = null;

		try {
			connection = (HttpURLConnection) url.openConnection();
		} catch (IOException e) {
			//msgBox = new MessageBox("Open URL connection error");
			System.out.println("problem with unkwon protocol");
		}

		try {
			connection.setRequestMethod("GET");
		} catch (ProtocolException e) {
			msgBox = new MessageBox("Setting order URL error");
		}
		connection.setReadTimeout(5000);
		connection.setConnectTimeout(5000);

		try {
			connection.connect();
			rawHttp = readReplyServer();
			connection.disconnect();
			connection = null;
			if (rawHttp != null)
				System.out.println("Fetcher successfully fetched " + url
						+ ", chars: " + rawHttp.length());
		} catch (IOException e) {
			// msgBox = new MessageBox("URL connecting error");
			System.out.println("Timeout in fetching");
		}
		return rawHttp;
	}

	private String readReplyServer() {
		// System.out.println("Fetcher.readReplyServer");
		String wholeInfo = null;
		String line;

		BufferedReader bufferReader = null;
		try {
			bufferReader = new BufferedReader(new InputStreamReader(
					connection.getInputStream()));
		} catch (IOException e) {
			// file doesn't exist
			// msgBox = new MessageBox("Get Input Stream Reader error ");
			// e.printStackTrace();
		}

		try {
			while (bufferReader != null
					&& (line = bufferReader.readLine()) != null) {
				wholeInfo += line;
				wholeInfo +='\n';
			}
		} catch (IOException e) {
			msgBox = new MessageBox("Reading line HTML error");
		}
		return wholeInfo;
	}
}
